part of 'page.dart';

class _LexicalItemAdditionPageWloc
    extends StatefulWloc<_LexicalItemAdditionPageWlocState, LexicalItemAdditionPage> {
  late final bloc.LexicalItemAdditionPageBloc _bloc;

  late final TextEditingController searchBarTextEditingController;
  late final FocusNode searchBarFocusNode;

  @override
  _LexicalItemAdditionPageWlocState get initialState {
    return _LexicalItemAdditionPageWlocStateFactory().create(
      blocState: _bloc.state,
    );
  }

  void onSearchBarTextChanged() {
    _bloc.requestLexicalItemSuggestion(
      prefix: searchBarTextEditingController.text,
    );
  }

  void onSearchBarTextSubmitted() {
    createCustomLexicalItem();
  }

  void createCustomLexicalItem() async {
    final searchBarHasFocus = searchBarFocusNode.hasFocus;

    final lexicalItemCreated = await showAppFullscreenModal<bool>(
      enableDrag: false,
      builder: (context) {
        final String lexicalItemTitle;
        final String lexicalItemTranslation;

        if (_bloc.state.isTitlePrefix) {
          lexicalItemTitle = _bloc.state.trimmedPrefix;
          lexicalItemTranslation = '';
        } else {
          lexicalItemTranslation = _bloc.state.trimmedPrefix;
          lexicalItemTitle = '';
        }

        return CustomLexicalItemCreationPage(
          lexicalItemTitle: lexicalItemTitle,
          lexicalItemTranslation: lexicalItemTranslation,
          customCollectionId: widget.pageModel.customCollectionId,
        );
      },
    );

    if (lexicalItemCreated == true) {
      searchBarTextEditingController.clear();
      onSearchBarTextChanged();
    }

    if (searchBarHasFocus) {
      searchBarFocusNode.requestFocus();
    }
  }

  void onExistingLexicalItemTilePressed({
    required String lexicalItemId,
  }) async {
    final searchBarHasFocus = searchBarFocusNode.hasFocus;

    final lexicalItemAdded = await showAppFullscreenModal<bool>(
      builder: (context) {
        return ExistingLexicalItemAdditionPage(
          lexicalItemId: lexicalItemId,
          customCollectionId: widget.pageModel.customCollectionId,
        );
      },
    );

    if (lexicalItemAdded == true) {
      searchBarTextEditingController.clear();
      onSearchBarTextChanged();
    }

    if (searchBarHasFocus) {
      searchBarFocusNode.requestFocus();
    }
  }

  void _onBlocStateChanged(bloc.LexicalItemAdditionPageBlocState blocState) {
    final updatedState = _LexicalItemAdditionPageWlocStateFactory().create(
      blocState: _bloc.state,
    );

    emit(updatedState);
  }

  @override
  void initState() {
    super.initState();

    _bloc = bloc.LexicalItemAdditionPageBloc(
      pageModel: widget.pageModel,
      learningContentBloc: core_di.sl(),
      rootNavigatorBloc: core_di.sl(),
      suggestLexicalItems: lexical_item_addition_page_di.sl(),
      handleLearningContentOuterUpdate: lexical_item_addition_page_di.sl(),
      getInitialData: lexical_item_addition_page_di.sl(),
    );

    _bloc.stream.listen(_onBlocStateChanged);

    searchBarTextEditingController = TextEditingController();
    searchBarFocusNode = FocusNode();

    WidgetsBinding.instance.addPostFrameCallback((_) {
      searchBarFocusNode.requestFocus();
    });
  }

  @override
  void dispose() {
    _bloc.close();
    searchBarTextEditingController.dispose();
    searchBarFocusNode.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return _LexicalItemAdditionPageView(
      wloc: this,
    );
  }
}
